package sjtu.ist.warehouse4ws.tool;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;



import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import org.xml.sax.SAXException;

import sjtu.ist.warehouse4ws.model.Permission;
import sjtu.ist.warehouse4ws.model.Permissionresources;

public class ParserPermission {
	
	public String parseNode(String tagName, Document doc){
		
		NodeList parentlist= doc.getElementsByTagName(tagName);      //获取该ID的标签
		  Element headnode= (Element) parentlist.item(0);                 //获取该ID标签的第一个Item
	      NodeList childlist=headnode.getChildNodes();                 //获取该ID标签第一个Item里面的结点
			if (childlist.getLength() != 0) {
				String tagvalue = childlist.item(0).getNodeValue(); // 取值
				if (tagvalue.isEmpty()) {
					return null;
				} else {
					return tagvalue;
				}
			} else {
				return null;
			}
	}
	public Permission parse(String rootPath) throws SAXException, IOException, ParserConfigurationException   {
	    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
	    
	    try{
		 	DocumentBuilder db = dbf.newDocumentBuilder();
		 	
		 	
		 	//Change2UTF.change(rootPath);  //change other coded file to UTF-8
		 	File file = new File(rootPath);
           
            Document doc = db.parse(file); 
            String id=parseNode("id",doc);
            String enName=parseNode("enName",doc);
            String cnName=parseNode("cnName",doc);
            String description=parseNode("description",doc);
            String permissionPackage=parseNode("permissionPackage",doc);
            
         
            
	       
	        Permission perm=new Permission();        //the start point of the process
            perm.setId(id);
            perm.setEnName(enName);
            perm.setCnName(cnName);
            perm.setDescription(description);
            perm.setPermissionPackage(permissionPackage);
            
            return perm;
	    }
            finally{}
    }
	public List<Permissionresources> parsePR(String rootPath) throws SAXException, IOException{
		  DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
	      
		try {
			DocumentBuilder db= dbf.newDocumentBuilder();
			//Change2UTF.change(rootPath);  //change other coded file to UTF-8
		 	File file = new File(rootPath);
           
            Document doc = db.parse(file); 
            String id=parseNode("id",doc);
            NodeList resources= doc.getElementsByTagName("string");
            if (resources.getLength()!=0){
            	List<Permissionresources> prs=new ArrayList<Permissionresources>();
                 for (int i=0;i<resources.getLength();i=i+1){
                	 Permissionresources permissionresources=new Permissionresources();
     	    	     Element valueNode =(Element) resources.item(i);
     	    	     NodeList valueList=valueNode.getChildNodes();
     	    	     permissionresources.setResourceid(valueList.item(0).getNodeValue());
     	    	     permissionresources.setPermissionid(id);
     	    	     prs.add(permissionresources);
     	       }
   		         return prs;
            }
           
			
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
		 	
		 	
		 
		
	}
	public static void main(String[] args) throws IOException, Exception {
		
		ParserPermission pp=new ParserPermission();
		String path="E:/runtime-EclipseApplication/demo2/组织视图/perms/c057019d-ce55-40a6-ac9b-bd1fa76bdac2.xml";
		pp.parse(path);
		


	}
	
}

